package com.rightsidetech.system.service.impl;

import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.SMSTemplate;
import com.rightsidetech.system.config.AliyunServer;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.EnumMessageType;
import com.rightsidetech.commom.utils.StringUtil;
import com.rightsidetech.system.dtos.request.AliPayRegReq;
import com.rightsidetech.system.dtos.request.SendValidCodeReq;
import com.rightsidetech.system.entity.SysAgentLoginInfo;
import com.rightsidetech.system.entity.SysSendMessageLog;
import com.rightsidetech.system.entity.store.SysStoreLoginInfo;
import com.rightsidetech.system.mapper.SysAgentLoginInfoMapper;
import com.rightsidetech.system.mapper.SysSendMessageLogMapper;
import com.rightsidetech.system.mapper.SysStoreLoginInfoMapper;
import com.rightsidetech.system.service.SendMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @Package: com.rightsidetech.system.service.impl
 * @ClassName: SendMessageServiceImpl
 * @Author: yinj
 * @CreateTime: 2021/11/04 15:10
 * @Description: 短信相关
 */
@Service("sendMessageServiceImpl")
public class SendMessageServiceImpl implements SendMessageService {

    private final static Logger logger = LoggerFactory.getLogger(SendMessageServiceImpl.class);

    @Resource
    private SysSendMessageLogMapper sysSendMessageLogMapper;
    @Resource
    private AliyunServer aliyunServer;
    @Resource
    private SysStoreLoginInfoMapper sysStoreLoginInfoMapper;
    @Resource
    private SysAgentLoginInfoMapper sysAgentLoginInfoMapper;

    @Override
    public ResponseData sendValidCode(SendValidCodeReq codeReq) {
        try {
            if (StringUtil.isNullorEmpty(codeReq.getMobileNo()) || StringUtil.isNullorEmpty(codeReq.getReqType())) {
                return new ResponseData(CodeIdEnum.ParameterMalformed);
            }
            String mobileNo = codeReq.getMobileNo();
            String reqType = codeReq.getReqType();
            Random random = new Random();
            int x = random.nextInt(899999);
            x = x + 100000;
            SysStoreLoginInfo storeLoginInfo = null;
            SysAgentLoginInfo sysAgentLoginInfo = null;
            SMSTemplate smsTemplate;
            switch (reqType) {
                case "StoreLogin":
                    storeLoginInfo = sysStoreLoginInfoMapper.selectByMobileNo(codeReq.getMobileNo());
                    if (storeLoginInfo == null) {
                        return ResponseData.fail(CodeIdEnum.NOACCOUNT);
                    }
                    smsTemplate = SMSTemplate.REGIST;
                    break;
                case "StoreBack":
                    storeLoginInfo = sysStoreLoginInfoMapper.selectByMobileNo(codeReq.getMobileNo());
                    if (storeLoginInfo == null) {
                        return ResponseData.fail(CodeIdEnum.NOACCOUNT);
                    }
                    smsTemplate = SMSTemplate.BACK;
                    break;
                case "AgentLogin":
                    sysAgentLoginInfo = sysAgentLoginInfoMapper.selectByMobileNo(codeReq.getMobileNo());
                    if (sysAgentLoginInfo == null) {
                        return ResponseData.fail(CodeIdEnum.NOACCOUNT);
                    }
                    smsTemplate = SMSTemplate.LOGIN;
                    break;
                case "AgentRegist":
                    sysAgentLoginInfo = sysAgentLoginInfoMapper.selectByMobileNo(codeReq.getMobileNo());
                    if (sysAgentLoginInfo == null) {
                        return ResponseData.fail(CodeIdEnum.NOACCOUNT);
                    }
                    smsTemplate = SMSTemplate.REGIST;
                    break;
                case "AgentBack":
                    sysAgentLoginInfo = sysAgentLoginInfoMapper.selectByMobileNo(codeReq.getMobileNo());
                    if (sysAgentLoginInfo == null) {
                        return ResponseData.fail(CodeIdEnum.NOACCOUNT);
                    }
                    smsTemplate = SMSTemplate.BACK;
                    break;
                default:
                    smsTemplate = SMSTemplate.COMMON;
                    break;
            }
            if (StringUtil.isNullorEmpty(smsTemplate)) {
                return ResponseData.fail("验证码发送失败");
            }
            aliyunServer.sendMsg(mobileNo, smsTemplate, StringUtil.getString(x));

            SysSendMessageLog sm = new SysSendMessageLog();
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            sm.setId(id);
            sm.setAddto(mobileNo);
            sm.setBackMsg("");
            sm.setCreateDate(new Date());
            sm.setCredits(0);
            sm.setCode(StringUtil.getString(x));
            sm.setMsgType(EnumMessageType.ValidateCodeMsg.getValue());
            sm.setMsg(StringUtil.getString(x));
            sm.setTimeout(10);
            sysSendMessageLogMapper.updateTimeoutByMobileNo(mobileNo);
            sysSendMessageLogMapper.insertSelective(sm);
            return ResponseData.success(sm.getId());
        } catch (Exception e) {
            logger.error("发送验证码失败", e);
            return new ResponseData(CodeIdEnum.FASONGYANZHENGMASHIBAI);
        }
    }

    @Override
    public ResponseData checkCerifyCode(AliPayRegReq aliPayRegReq) {
        try {
            SysSendMessageLog sm = sysSendMessageLogMapper.findByAddTo(aliPayRegReq.getPhone());
            if (StringUtil.isNullorEmpty(sm) || !aliPayRegReq.getVerifyCode().equals(sm.getCode())) {
                return new ResponseData(CodeIdEnum.VERIFY_CODE_ERROR);
            }
            if (!sm.getCode().equals(aliPayRegReq.getVerifyCode())) {
                return new ResponseData(CodeIdEnum.VERIFY_CODE_ERROR);
            }
            //验证验证码是否过期
            Date afterDate = new Date(sm.getCreateDate().getTime()
                    + sm.getTimeout() * 60000);
            if (new Date().after(afterDate)) {
                return new ResponseData(CodeIdEnum.TimeOut);
            }
        } catch (Exception e) {
            logger.error("传过来的参数不满足要求", e);
            return new ResponseData(CodeIdEnum.ParameterMalformed);
        }
        return new ResponseData(CodeIdEnum.Success);
    }
}
